package com.duan.config.interceptor;

import com.duan.pojo.User;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession(false); // 如果当前Session没有就位null
        if(session != null){ //获取登录时设置的用户信息
            User user = (User)session.getAttribute("currentUser");
            if(user != null){ //登录了，允许访问
                return true;
            }
        }

        response.sendRedirect("/index.html");
        return false;
    }
}
